import * as echarts from '../../ec-canvas/echarts'
const http = require('../../utils/http.js'); //相对路径
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  attached: function () {
    if(wx.getStorageSync('Identity') == 2 || wx.getStorageSync('Identity') == 3) return;
    var that = this
    that.setData({
      appID: wx.getAccountInfoSync().miniProgram.appId
    })
    http.postRequest('/mini/v1/home/boss', {}, res => {
      this.ecComponent = this.selectComponent("#mychart-dom-line")
      if (res.code == 0) {
        for (var i in res.data.week_data) {
          this.data.weekData.push(res.data.week_data[i].amount)
          this.data.abscissa.push(res.data.week_data[i].date)
        }
        let weekNum = 0;
        for(let i in res.data.week_data) {
          weekNum+= Number(res.data.week_data[i].amount);
        }
        
        wx.setNavigationBarTitle({
          title: res.data.shop_name
        })
        // 线图
        let pieOptions = {
          title: {
            left: 'center'
          },
          grid: {
            y2: 20,
            x: 50,
            x2: 40,
            y: 20
          },
          color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
          tooltip: {
            show: true,
            trigger: 'axis'
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.data.abscissa,
            axisLabel: {
              textStyle: {
                color: '#259BFF',
                fontSize:'12',
              }
            },
            axisLine:{
              lineStyle:{
                color:'#259BFF',
                width: 2
              }
            }
          },
          axisLabel: {
            color: '#FF6F26',
            formatter: function(value,index) {
              var value;
              if (value >=1000) {
                value = parseInt(Number(value)/1000)+'k';
              }else if(value <1000){
                value = value;
              }
              return value;
            }
          },
          yAxis: [{
            type: 'value',
            interval: parseInt(Math.max(...this.data.weekData)/5),
            max: Math.max(...this.data.weekData),
            min: 0,
            axisLine:{
              lineStyle:{
                color:'#FF6F26',
                fontSize:'12',
                width: 2
              }
            }
          }],
          series: [{
            name: '营业额',
            type: 'line',
            smooth: true,
            data: this.data.weekData,
            lineStyle: {
              color: "#FF6F26",
              fontSize:'12',
              width: 2
            }
          }]
        }
        // 饼图
        let lineOptions = {
          color: ['#39B54A', '#259BFF', '#FF6F26'],
          series: [{
            type: 'pie',
            radius: '55%',
            center: ['45%', '50%'],
            data: this.data.pieData,
            selectedMode: 'single',
            animation: false,
            label: {
              formatter: '{b}{c}',
              position: 'outer',
              alignTo: 'none',
              bleedMargin: 5
            }
          }]
        }
        let name = ''
        for (var i in res.data.today_data) {
          if (res.data.today_data[i].type == 1) {
            name = "材料"
          } else if (res.data.today_data[i].type == 2) {
            name = "项目"
          } else if (res.data.today_data[i].type == 3) {
            name = "会员卡"
          } else if (res.data.today_data[i].type == 4) {
            name = "储值卡"
          } else if (res.data.today_data[i].type == 5) {
            name = "附件费"
          }
          this.data.pieData.push({
            name: name,
            value: res.data.today_data[i].real_amount
          })
        }
        let num = []
        num.push(res.data.wait_execute, res.data.wait_finish, res.data.wait_clearing)
        num = JSON.stringify(num)
        that.setData({
          bossInfo: res.data,
          pieOptions: pieOptions,
          lineOptions: lineOptions,
          num: num,
          weekNum: weekNum
        }, () => {
          // 设置图标option数据
          this.init(pieOptions)
        })
      }
    }, 500)
  },
  /**
   * 组件的初始数据
   */
  data: {
    current: 0,
    pieData: [],
    weekData: [],
    abscissa: [],
    date: true,
    ec: {
      lazyLoad: true
    },
    appID: '',
    bossInfo: '',
    num: [],
    weekNum: 0, //周数据
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击通知组件跳转到预约页面
    reservation() {
      wx.navigateTo({
        url: '/pages/classify/fuwu',
      })
    },
    
    init(options) {
      this.ecComponent.init((canvas, width, height, dpr) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        chart.setOption(options)
        // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
        this.chart = chart;
        return chart;
      })
    },

    skip(e) {
      const name = e.currentTarget.dataset.name;
      if (name == 'kaidan') {
        wx.navigateTo({
          url: '/pages/scan/scan',
        })
      } else if (name == 'kehu') {
        wx.navigateTo({
          url: '/pages/classify/kehu',
        })
      } else if (name == 'fuwu') {
        wx.navigateTo({
          url: '/pages/classify/fuwu',
        })
      } else if (name == 'chejian') {
        wx.navigateTo({
          url: '/pages/classify/chejian',
        })
      } else if (name == 'yinyon') {
        wx.navigateTo({
          url: '/pages/allApp/allApp',
        })
      }
    },

    statepage(e) {
      const name = e.currentTarget.dataset.name;
      wx.navigateTo({
        url: '/pages/index/work/work?name=' + name + '&num=' + this.data.num
      })
    },
    openCamera() {
      wx.navigateTo({
        url: '/pages/scan/scan'
      })
    },
    changeType(e) {
      let ec = this.data.ec
      let date = !this.data.date
      ec['lazyLoad'] = true
      this.setData({
        ec: ec,
        date: date
      })
      if (date) {
        this.init(this.data.pieOptions)
      } else {
        if(!this.data.bossInfo.week_data[6].amount) return;
        this.init(this.data.lineOptions)
      }
    },
  }
})